#include "TIMER_Init.h"

void TIMER_Init_Config(void)
{
	TIM_TimeBaseInitTypeDef     TIM_TimeBaseStructure;
	TIM_ICInitTypeDef           TIM_ICInitStructure;
	GPIO_InitTypeDef            GPIO_InitStructure;
	
	/*---------------------------GPIO结构体初始化-------------------------------------------------*/
	//开始GPIO端口时钟
	RCC_AHB1PeriphClockCmd(ADVANCE_IC_GPIO_CLK,ENABLE);
	
	//定时器复用引脚
	
	GPIO_PinAFConfig(ADVANCE_IC_PORT,ADVANCE_IC_PINSOURCE,ADVANCE_IC_AF);
	
	//高级控制定时器输入捕获引脚
	
	GPIO_InitStructure.GPIO_Pin=ADVANCE_IC_PIN;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_NOPULL;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;
	GPIO_Init(ADVANCE_IC_PORT,&GPIO_InitStructure);
	
	//开启TIMx_CLK
	//高级定时器时钟源TIMxCLK=2*PCLK2=168MHz
	RCC_APB2PeriphClockCmd(ADVANCE_TIM_CLK,ENABLE);


	/*-------------------------时基结构体初始化-------------------------------------------*/
	//累计TIM_Period个后产生一个更新或中断
	TIM_TimeBaseStructure.TIM_Period=ADVANCE_TIM_PERIOD;
	//驱动CNT计数器的时钟=TIMxCLK/(psc+1)
	TIM_TimeBaseStructure.TIM_Prescaler=ADVANCE_TIM_PSC;
	//时钟分频因子，配置死区时间时需要用到
	TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
	//计数方式
	TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
	//重复计数器的值
	TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
	//初始化定时器
	TIM_TimeBaseInit(ADVANCE_TIM,&TIM_TimeBaseStructure);
	
	/*----------------------------输入捕获结构体初始化-------------------------------------*/
	// 使用输入模式时，需要占用两个捕获寄存器，一个测周期，另外一个测占空比
	// 捕获通道 IC1 配置
	// 选择捕获通道

	TIM_ICInitStructure.TIM_Channel=ADVANCE_IC_CHANNEL;
	// 设置捕获的边沿
	TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
	// 设置捕获通道的信号来自于哪个输入通道，有直连和非直连两种
	TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
	// 1 分频，即捕获信号的每个有效边沿都捕获
	TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
	// 不滤波
	TIM_ICInitStructure.TIM_ICFilter = 0xF;
	// 初始化 PWM 输入模式
	TIM_PWMIConfig(ADVANCE_TIM, &TIM_ICInitStructure);
 
	// 当工作做 PWM 输入模式时,只需要设置触发信号的那一路即可（用于测量周期）
	// 另外一路（用于测量占空比）会由硬件自带设置，不需要再配置
	// 选择输入捕获的触发信号
	TIM_SelectInputTrigger(ADVANCE_TIM, TIM_TS_TI1FP1);
	 
	// 选择从模式: 复位模式
	// PWM 输入模式时,从模式必须工作在复位模式，当捕获开始时,计数器 CNT 会被复位
	TIM_SelectSlaveMode(ADVANCE_TIM, TIM_SlaveMode_Reset);
	TIM_SelectMasterSlaveMode(ADVANCE_TIM,TIM_MasterSlaveMode_Enable);
	 
	// 使能捕获中断,这个中断针对的是主捕获通道（测量周期那个）
	TIM_ITConfig(ADVANCE_TIM, TIM_IT_CC1, ENABLE);
	TIM_ClearITPendingBit(ADVANCE_TIM, TIM_IT_CC1);
	 
	// 使能高级控制定时器，计数器开始计数
	TIM_Cmd(ADVANCE_TIM, ENABLE);
}
	static void TIMx_NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;
	// 设置中断组为 0
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
	// 设置中断来源
	NVIC_InitStructure.NVIC_IRQChannel = ADVANCE_TIM_IRQn;
	// 设置抢占优先级
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	// 设置子优先级
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
	NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
	NVIC_Init(&NVIC_InitStructure);
}
volatile	uint32_t this_time_CH1 ;
volatile	uint32_t last_time_CH1 ;
volatile	uint8_t capture_number_CH1 ;
volatile double f;
volatile double fr;            
volatile	double ConUploadFloat;       //上传的浮点数电导率
volatile	float FreUploadFloat;       //上传的浮点数频率	
volatile	uint8_t CH1_Cycles_Count;
volatile	double G;                   //电导率值
void ADVANCE_TIM_IRQHandler (void)
{
	 //频率缓冲区计数

		
	if (TIM_GetITStatus(TIM8,TIM_IT_Update) != RESET)       //计数器更新中断
	{
		  TIM_ClearITPendingBit(TIM8, TIM_IT_Update);
		  
		  CH1_Cycles_Count++;
	}
	if(TIM_GetITStatus(TIM8, TIM_IT_CC1) != RESET)   //通道1脉冲检测中断
	{
			TIM_ClearITPendingBit(TIM8, TIM_IT_CC1);
			if(capture_number_CH1 == 0)                          //收到第一个上升沿
			{
				capture_number_CH1 = 1;
				last_time_CH1 = TIM_GetCapture1(TIM8);
			}
			else if(capture_number_CH1 == 1)                     //收到第二个上升沿
			{
				//capture_number_CH1 = 0;
				this_time_CH1 = TIM_GetCapture1(TIM8);
				f = this_time_CH1 - last_time_CH1 + 65536 * CH1_Cycles_Count;
				G=f/1000;
				ConUploadFloat=6.30354567028508-4.06468223915935*G+2.16278309337694*G*log(G)-7.1551351900011/(G*G);
				FreUploadFloat=f;
                
				CH1_Cycles_Count = 0;
				last_time_CH1 = this_time_CH1;	
			}							
	}
}
void TIM_Init(void)
 {

 
 
 /* 高级定时器中断优先级配置 */
	TIMx_NVIC_Configuration();
 
 
	TIMER_Init_Config();
 

 }
void Conductivity(void)
{
	NVIC_EnableIRQ(ADVANCE_TIM_IRQn);
}
